<?php
include_once 'config.php';
$KeyTopicAreaId = 'topicarea_id';
$KeyTopicAreaName='topicarea_name';
$KeyTopicClassId = 'topicclass_id';
$KeyTopicClassName = 'topicclass_name';
$KeyTopicClassTopicsCount='topicclass_topics_count';
$KeyTopicClassAllCount='topicclass_all_count';
$KeyTopicClassLastTime = 'topicclass_lasttime';
$KeyTopicClassImgUrl ='topicclass_img_url'; 
$KeyTopicTitle = 'topic_title';
$KeyUsersUserId = 'users_user_id';
$KeyTopicCreateTime = 'topic_createtime';
$KeyTopicReplyCount = 'topic_reply_count';
$KeyTopicClickCount = 'topic_click_count';
if($_GET!=NULL){
	if(isset($_GET['area'])){
		$Area = GetSingleArea($_GET['area']);
	}
}
else{
	$Area = GetAllArea();
}
//foreach ($Area as $key1 => $value) {
//	foreach ($Area[$key1] as $key2 => $value) {
//		echo $Area[$key1][$key2];
//		echo "			";
//	}
//	$TopicClass = GetTopicClass($Area[$key1]['topicarea_id']);
//	foreach ($TopicClass as $key3 => $value3){
//		foreach ($TopicClass[$key3] as $key4 => $value4){
//			echo $value4;
//		}
//	}
//	echo "<br>";
//}

function GetSingleArea($TopicAreaId){
	$Link_mysql = mysqli_connect(Server, User, Pwd, Database);
	$SqlSelect = "select * from topicareas where topicarea_id = ".$TopicAreaId;
	$Result = $Link_mysql -> query($SqlSelect);
	$Area = mysqli_fetch_all($Result, MYSQLI_ASSOC);
	mysqli_close($Link_mysql);
	return $Area;
}

function GetAllArea(){
	$Link_mysql = mysqli_connect(Server, User, Pwd, Database);
	$SqlSelect = "select * from topicareas";
	$Result = $Link_mysql -> query($SqlSelect);
	$Area = mysqli_fetch_all($Result, MYSQLI_ASSOC);
//		$_SESSION['user'] = json_encode($UserInfo);
	mysqli_close($Link_mysql);
	return $Area;
}
function GetTopicClass($TopicAreaId){
	$Link_mysql = mysqli_connect(Server, User, Pwd, Database);
	$SqlSelect = "select * from topicclass where topicareas_topicarea_id = '".$TopicAreaId."'";
	$Result = $Link_mysql -> query($SqlSelect);
	$Class = mysqli_fetch_all($Result, MYSQLI_ASSOC);
	mysqli_close($Link_mysql);
	return $Class;
}
function GetMaster($TopicAreaId){
	$Link_mysql = mysqli_connect(Server, User, Pwd, Database);
	$SqlSelect = "select user_name from users where user_id = '".$TopicAreaId."'";
	$Result = $Link_mysql -> query($SqlSelect);
	$Master = mysqli_fetch_array($Result, MYSQLI_ASSOC);
	mysqli_close($Link_mysql);
	return $Master;
}

function GetAllTopics($TopicClassId){
	$Link_mysql = mysqli_connect(Server, User, Pwd, Database);
	$SqlSelect = "select topic_title,topic_createtime,topic_click_count,topic_reply_count,users_user_id,topic_lasttime from topics where topicclass_topicclass_id = '".$TopicClassId."' ORDER BY `topics`.`topic_createtime` DESC";
	$Result = $Link_mysql -> query($SqlSelect);
	$Topics =  mysqli_fetch_all($Result, MYSQLI_ASSOC);
//	foreach ($Topics as $key => $value) {
//		foreach($Topics[$key] as $keys => $value1){
//			$SqlSelectUserName = "select user_name from users where user_id = '".$Topics[$key][$KeyUsersUserId]."'";
//			$Result = $Link_mysql -> query($SqlSelectUserName);
//			
//		}
//	}
	mysqli_close($Link_mysql);
	return $Topics;
}
function GetTopicClassByTopicClassId($TopicClassId){
	$Link_mysql = mysqli_connect(Server, User, Pwd, Database);
	$SqlSelect = "select * from topicclass where topicclass_id = '".$TopicClassId."'";
	$Result = $Link_mysql -> query($SqlSelect);
	$TopicClass =  mysqli_fetch_array($Result, MYSQLI_ASSOC);
//	foreach ($Topics as $key => $value) {
	mysqli_close($Link_mysql);
	return $TopicClass;
}
function GetTopicClassMaster($TopicClassId){
	$Link_mysql = mysqli_connect(Server, User, Pwd, Database);
	$SqlSelect = "select user_id,user_name from users where user_id in(select users_user_id from topicareas where topicarea_id in(select topicareas_topicarea_id from topicclass where topicclass_id = '".$TopicClassId."'))";
	$Result = $Link_mysql -> query($SqlSelect);
	$Master =  mysqli_fetch_array($Result, MYSQLI_ASSOC);
	echo mysqli_error($Link_mysql);
	mysqli_close($Link_mysql);
	return $Master;
}
function GetTopicAreaByTopicClassId($TopicClassTopicAreaId){
	$Link_mysql = mysqli_connect(Server, User, Pwd, Database);
	$SqlSelect = "select topicarea_id, topicarea_name from topicareas where topicarea_id in(select topicareas_topicarea_id from topicclass where topicclass_id = '".$TopicClassTopicAreaId. "')";
	$Result = $Link_mysql -> query($SqlSelect);
	$TopicArea =  mysqli_fetch_array($Result, MYSQLI_ASSOC);
//	foreach ($Topics as $key => $value) {
	mysqli_close($Link_mysql);
	return $TopicArea;
}
?>